//
//  CSGXHomeViewController.m
//  CarSalesMS_GCX
//
//  Created by Tony on 2016/10/21.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXHomeViewController.h"
#import "CSFMDBTool.h"
#import "CSGXHeadView.h"
#import "CSGXBodyView.h"
#import "CSGXHomeSaleController.h"
#import "CSGXHomeSearchController.h"
#import "CSGXDailyCashController.h"
#import "CSGXInventoryManageController.h"
#import "CSGXInventoryCheckController.h"

#import "TUserInfoModel.h"
#import "TSaleInfoModel.h"

#import "TUserInfoDAL.h"
#import "TSaleInfoDAL.h"

@interface CSGXHomeViewController () <CSGXBodyViewBackDelegate, UIScrollViewDelegate>

@property(nonatomic, assign) BOOL isOpened;

@property (nonatomic, strong) TSaleInfoDAL * saleDal;

@property (nonatomic, copy) NSString * userNo;
@property (nonatomic, copy) NSString * businessNo;
@property (nonatomic, copy) NSString * warehouseNo;

#pragma mark data
@property (nonatomic, strong) TUserInfoModel * userModel; // 当前登录用户model信息
@property (nonatomic, strong) CSGXHeadModel * headModel; // model for headView

@property (nonatomic, strong) NSArray<TSaleInfoModel *> *saleModelList;


@property (nonatomic, strong) CSGXHeadView * headView;

@property (nonatomic, strong) CSGXBodyView * bodyView;

@property (nonatomic, strong) CSFMDBTool * dbTool;

@property (nonatomic, weak) UIPageControl *pcHome;


@end

@implementation CSGXHomeViewController

+ (void)initialize {
    NSString * docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString * dbName = [docPath stringByAppendingPathComponent:@"LKFX.db"];
    
    NSLog(@"%@", dbName);
    
    [[CSFMDBTool SharedFmdbTool] openDBwithDbName:dbName];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.bodyView.elementList = @[@[@"车销销售", @"车销查询", @"现金日报", @"库存管理", @"库存盘点"],
                                  @[@"2-1", @"2-2"],
                                  @[@"3-1", @"3-2", @"3-3", @"3-4"]
                                  ];
    
//    self.pcHome.backgroundColor = [UIColor redColor];
    self.pcHome.numberOfPages = self.bodyView.elementList.count;
    self.pcHome.pageIndicatorTintColor = [UIColor grayColor];
    self.pcHome.currentPageIndicatorTintColor = [UIColor blueColor];
    
    // 默认顶下来，贴到navBar下边
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
    
    self.dbTool = [CSFMDBTool SharedFmdbTool];
    
    if (![self loadData_login]) {
        return;
    } //test signin
    
    [self loadData_home];
    
    [self loadData_sale];
    
    
    
}

#pragma mark - Actions

- (void)signout:(UIBarButtonItem *)btnBar {
    NSLog(@"SignOut");
    
    [CSGXProgressTool showSuccessWithStatus:@"测试：离店成功!" delay:1];
}

- (void)testInsertIntoDB {
    [[CSFMDBTool SharedFmdbTool] insertTestDataIntoDB];
    
    [self loadData_login];
    [self loadData_home];
}

#pragma mark - methods
- (BOOL)setTitleStr {
    if (self.userModel == nil || self.userModel.uUserName == nil || self.userModel.uUserName.isEmpty) {
        self.title = @"你未登录！";
        return NO;
    }
    self.title = [NSString stringWithFormat:@"欢迎业务员：%@", self.userModel.uUserName];
    return YES;
}

- (void)jumpToViewControllerPage:(NSInteger)page elementIndex:(NSInteger)elementIndex {
    //
    if (page == 0) {
        //
        if (elementIndex == 0) {
            //
            CSGXHomeSaleController * saleVc = [CSGXHomeSaleController saleVcWithWarehouseNo:@"CK001"];
            saleVc.isReceivedHome = YES;
            [self pushViewController:saleVc];
        }else if (elementIndex == 1) {
            //
            CSGXHomeSearchController * searchVc = [[CSGXHomeSearchController alloc] init];
            [self pushViewController:searchVc];
        }else if (elementIndex == 2) {
            //
            CSGXDailyCashController *dailyCashVc = [[CSGXDailyCashController alloc] init];
            [self pushViewController:dailyCashVc];
        }else if (elementIndex == 3) {
            //
            CSGXInventoryManageController *inventoryManageVc = [[CSGXInventoryManageController alloc] init];
            [self pushViewController:inventoryManageVc];
        }else if (elementIndex == 4) {
            //
            CSGXInventoryCheckController *inventoryCheckVc = [[CSGXInventoryCheckController alloc] init];
            [self pushViewController:inventoryCheckVc];
        }else if (elementIndex == 5) {
            //
            
        }else if (elementIndex == 6) {
            //
            
        }else if (elementIndex == 7) {
            //
            
        }else if (elementIndex == 8) {
            //
            
        }
    }else if (page == 1) {
        //
        if (elementIndex == 0) {
            //
            
        }else if (elementIndex == 1) {
            //
            
        }else if (elementIndex == 2) {
            //
            
        }else if (elementIndex == 3) {
            //
            
        }else if (elementIndex == 4) {
            //
            
        }else if (elementIndex == 5) {
            //
            
        }else if (elementIndex == 6) {
            //
            
        }else if (elementIndex == 7) {
            //
            
        }else if (elementIndex == 8) {
            //
            
        }
        
    }else if (page == 2) {
        //
        if (elementIndex == 0) {
            //
            
        }else if (elementIndex == 1) {
            //
            
        }else if (elementIndex == 2) {
            //
            
        }else if (elementIndex == 3) {
            //
            
        }else if (elementIndex == 4) {
            //
            
        }else if (elementIndex == 5) {
            //
            
        }else if (elementIndex == 6) {
            //
            
        }else if (elementIndex == 7) {
            //
            
        }else if (elementIndex == 8) {
            //
            
        }
        
    }else if (page == 3) {
        //
        if (elementIndex == 0) {
            //
            
        }else if (elementIndex == 1) {
            //
            
        }else if (elementIndex == 2) {
            //
            
        }else if (elementIndex == 3) {
            //
            
        }else if (elementIndex == 4) {
            //
            
        }else if (elementIndex == 5) {
            //
            
        }else if (elementIndex == 6) {
            //
            
        }else if (elementIndex == 7) {
            //
            
        }else if (elementIndex == 8) {
            //
            
        }
        
    }// if page  end
}

- (void)pushViewController:(UIViewController *)vc {
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - delegates
- (void)bodyView:(CSGXBodyView *)bodyView page:(NSInteger)page elementIndex:(NSInteger)elementIndex {
    //
    [self jumpToViewControllerPage:page elementIndex:elementIndex];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger page = self.bodyView.contentOffset.x / [UIScreen mainScreen].bounds.size.width + 0.5;
//    NSLog(@"page: %@", @(page).description);
    
    self.pcHome.currentPage = page;
}


#pragma mark - setupUI
- (void)setNavBar {
    self.title = @"欢迎业务员：";
    self.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc] initWithTitle:@"离店退出" style:UIBarButtonItemStylePlain target:self action:@selector(signout:)];
    
    // 设置导航栏背景颜色
    self.navigationController.navigationBar.barTintColor = kColorBar;
    // 设置主题颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    // 设置返回按钮
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:nil];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"插入测试数据" style:UIBarButtonItemStylePlain target:self action:@selector(testInsertIntoDB)];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavBar];
    
    // headerView
    self.headView = [[CSGXHeadView alloc] init];
    [self.view addSubview:self.headView];
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(0);
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(150);
    }];
    
    //vLocation
    UIView *vLocation = [[UIView alloc] init];
    [self.view addSubview:vLocation];
    
    vLocation.backgroundColor = kColorBar;
    
    [vLocation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom);
        make.leading.trailing.equalTo(self.view).mas_offset(0);
        make.height.mas_offset(30);
    }];
    
    UILabel *lblLocationTitle = [UILabel gx_labelWithText:@"定位：" fontSize:15 color:[UIColor whiteColor]];
    [vLocation addSubview:lblLocationTitle];
    
    [lblLocationTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(vLocation);
        make.left.equalTo(vLocation).mas_offset(5);
    }];

    // bodyView
    self.bodyView = [[CSGXBodyView alloc] init];
    [self.view addSubview:self.bodyView];
    
    self.bodyView.delegate = self;
    
    [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vLocation.mas_bottom);
        make.leading.bottom.trailing.equalTo(self.view);
    }];
    self.bodyView.bodyViewDelegate = self;
    
    //pageControl
    UIPageControl *pcHome = [[UIPageControl alloc] init];
    [self.view addSubview:pcHome];
    self.pcHome = pcHome;
    
    [pcHome mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view).mas_offset(0);
        make.bottom.equalTo(self.view).mas_offset(-15);
        make.size.mas_offset(CGSizeMake(100, 20));
    }];
    
    [self.view layoutIfNeeded];
    
}

#pragma mark - loadData
- (BOOL)loadData_login {
    self.userNo = @"gcx";
    self.warehouseNo = @"CK001";
    self.businessNo = @"JLJ001";
    
    self.userModel = [TUserInfoDAL loadUserModelWithUserNo:self.userNo];
    self.userModel.uUserName = @"gcx";
   
    // 存入沙盒
    [CSUserDefaultsTool setUserNo:self.userModel.uUserNo];
    [CSUserDefaultsTool setUserName:self.userModel.uUserName];
    [CSUserDefaultsTool setBusinessmanNo:self.businessNo];
    [CSUserDefaultsTool setBusinessman:@"佳乐家001"];
    [CSUserDefaultsTool setWarehouseNo:self.userModel.uWarehouseNo];
    [CSUserDefaultsTool setWarehouse:@"京京车仓库001"];
    
    return [self setTitleStr];
}

- (void)loadData_home {
    self.headModel = [CSGXHeadModel modelWithUserNo:self.userNo];
    self.headModel.businessNo = self.businessNo;
    self.navigationItem.rightBarButtonItem.enabled = self.headModel ? NO : YES;
    self.headView.headModel = self.headModel;
}

- (void)loadData_sale {
    // loadData based userNo and businessNo.
    self.saleModelList = [TSaleInfoDAL loadListFromTSaleInfoWithUserNo:self.userNo businessNo:self.businessNo];
}



@end
